package cn.fuego.busrental.ui.logo;

import java.util.Arrays;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import cn.fuego.busrental.R;
import cn.fuego.busrental.ui.Frament.HomeFragment;
import cn.fuego.busrental.ui.Frament.MainTabbarActivity;
import cn.fuego.common.log.FuegoLog;
import cn.fuego.common.util.validate.ValidatorUtil;
import cn.fuego.misp.constant.MispErrorCode;
import cn.fuego.misp.service.MemoryCache;
import cn.fuego.misp.service.http.MispHttpHandler;
import cn.fuego.misp.service.http.MispHttpMessage;
import cn.fuego.misp.ui.base.MispBaseActivtiy;
import cn.fuego.misp.ui.util.StrUtil;
import cn.fuego.misp.webservice.json.MispBaseReqJson;
import cn.fuego.misp.webservice.json.MispBaseRspJson;
import cn.fuego.misp.webservice.up.model.LoginRsp;
import cn.fuego.misp.webservice.up.model.base.UserJson;



public class LoginActivity extends MispBaseActivtiy 
{
	FuegoLog log = FuegoLog.getLog(LoginActivity.class);
	private static final int JUMP_FOR_REGIST=1;
    private EditText textName,textPwd;
    public static ProgressDialog proDialog;
	private Integer[] buttonIDList = new Integer[]{R.id.user_login_submit,R.id.user_login_find_password,R.id.user_login_to_register};
	public static String JUMP_SOURCE = "jump_source";
	public static LoginActivity instance;
	private int hasFocusID=0;
	
	@Override
	public void initRes()
	{
		
		this.activityRes.setAvtivityView(R.layout.user_login);
		
		this.activityRes.setButtonIDList(Arrays.asList(buttonIDList));
	
	}
	
    @Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		textName = (EditText) findViewById(R.id.user_login_name);
		textPwd =(EditText) findViewById(R.id.user_login_password);
			
/*		View nameView = (View) findViewById(R.id.user_login_name_view);
		View pswdView = (View) findViewById(R.id.user_login_pswd_view);
		nameView.getBackground().setAlpha(100);//0~255透明度值 
		pswdView.getBackground().setAlpha(100);//0~255透明度值 */
		instance=this;
	}
   

	@Override
	public void onClick(View v)
	{
		
		int id = v.getId();
		if (id == R.id.user_login_submit) {
			MainTabbarActivity.jump(this);
			finish();
//			log.info("Submit button clicked...");
//			String userName = textName.getText().toString().trim();
//			// password =textPwd.getText().toString();
//			String password = textPwd.getText().toString().trim();
//			String pswd = StrUtil.MD5(password);
//			proDialog =ProgressDialog.show(this, "请稍等", "登录信息验证中……");
//			checkLogin(userName,pswd);
		} else if (id == R.id.user_login_find_password) {
			log.info("Find-password button clicked...");
			Intent i = new Intent();
			i.putExtra(UserRegisterActivity.OPERTATE_NAME, UserRegisterActivity.FIND_PWD);
			i.setClass(this, UserRegisterActivity.class);
			this.startActivity(i);
		} else if (id == R.id.user_login_to_register) {
			log.info("Register button clicked...");
			Intent i = new Intent();
			i.putExtra(UserRegisterActivity.OPERTATE_NAME, UserRegisterActivity.REGISTER);
			i.setClass(this, UserRegisterActivity.class);
			this.startActivity(i);
		}
	}

 
	/**
	 * 登录
	 * @param userName
	 * @param password
	 */
	public void checkLogin(String userName,String password)
	{
		if(!ValidatorUtil.isEmpty(userName)&&!ValidatorUtil.isEmpty(password))
		{	
			MispBaseReqJson req = new MispBaseReqJson();
			UserJson json= new UserJson();
			json.setUser_name(userName);
			json.setPassword(password);
			req.setObj(json);
//			WebServiceContext.getInstance().getMispUserManageRest(new MispHttpHandler(){
//				@Override
//				public void handle(MispHttpMessage message)
//				{
//					proDialog.dismiss();
//					if(message.isSuccess()) {
//						LoginRsp rsp = (LoginRsp) message.getMessage().obj;
//						if(!ValidatorUtil.isEmpty(rsp.getToken())&& null != rsp.getObj())
//						{
//							MemoryCache.setToken(rsp.getToken());
//							UserJson user = rsp.GetReqCommonField(UserJson.class);
//							//加载用户
//							loadCustomer(user);
//							//登录环信
//							LoginChat(user.getUser_name(),StrUtil.MD5(user.getPassword()));
//						}
//						else
//						{
//							showMessage(MispErrorCode.ERROR_LOGIN_FAILED);
//						}
//					} else {
//						showMessage(message);
//					}
//				}
//			}).login(req);
		}
		else
		{
			showMessage("输入不能为空！");
			proDialog.dismiss();
		}

	}



	public  void LoginChat(String userName,String password)
	{
		
		log.info("login EM service, name = "+userName);	

//		EMChatManager.getInstance().login(userName,password,new EMCallBack() {//回调
//			@Override
//			public void onSuccess() {
//				runOnUiThread(new Runnable() {
//					public void run() {		
//						Log.d("main", "登陆聊天服务器成功！");
//						EMGroupManager.getInstance().loadAllGroups();
//						EMChatManager.getInstance().loadAllConversations();
//						//注册一个监听连接状态的listener
//						EMChatManager.getInstance().addConnectionListener(new IMConnectionListener());
//					}
//				});
//			}
//			@Override
//			public void onProgress(int progress, String status) {				
//			}
//			@Override
//			public void onError(int code, String message) {				
//				Log.d("main", "登陆聊天服务器失败！");
//			}
//		});	
	}

	//实现ConnectionListener接口
//	private class IMConnectionListener implements EMConnectionListener {
//	    @Override
//		public void onConnected() {
//		}
//		@Override
//		public void onDisconnected(final int error) {
//			runOnUiThread(new Runnable() {
//
//				@Override
//				public void run() {
//					if(error == EMError.USER_REMOVED){
//						// 显示帐号已经被移除
//					}else if (error == EMError.CONNECTION_CONFLICT) {
//						// 显示帐号在其他设备登陆
//					} else {
//					if (NetUtils.hasNetwork(LoginActivity.this))
//						//连接不到聊天服务器
//						log.error("连接不到聊天服务器");
//						
//					else
//						//当前网络不可用，请检查网络设置
//						log.error("当前网络不可用，请检查网络设置");
//					}
//				}
//			});
//		}
//	}

	private void loadCustomer(final UserJson user)
	{

//		MispBaseReqJson req = new MispBaseReqJson();
//		req.setObj(user.getUser_id());
//		WebServiceContext.getInstance().getCustomerManageRest(new MispHttpHandler(){
//			@Override
//			public void handle(MispHttpMessage message)
//			{
//				if(message.isSuccess())
//				{
//					MispBaseRspJson rsp = (MispBaseRspJson) message.getMessage().obj;
//					CustomerJson customer = rsp.GetReqCommonField(CustomerJson.class);
//					if(null!=customer){
//						
//						if(ValidatorUtil.isEmpty(customer.getNickname()))
//						{							
//							UserInfoActivity.jumpToHome(LoginActivity.this, customer);
//							AppCache.getInstance().update(MemoryCache.getToken(),user,customer);
//						}
//						else
//						{
//							StartActivity.Sign(customer.getUser_id());
//							loginSuccess(MemoryCache.getToken(),user,customer);
//						}
//						finish();
//					}else{
//						log.error("no customer...");
//					}
//				}
//				else
//				{
//					showMessage(message);
//				}
//			}
//		}).Show(req);

	}
	
//	private void loginSuccess(String token,UserJson user,CustomerJson customer)
//	{
// 		//AppCache.getInstance().update(token,user, customer);
// 		jumpToSource();
//		this.finish();
//	}
	private void jumpToSource()
	{
		MainTabbarActivity.jump(this, HomeFragment.class, 1);
	}
	
	public String getDeviceID()
	{
		TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getDeviceId();
	}

	/**
	 * 
	 * @param activity	上下文
	 * @param clazz		可能返回的Activity 参数
	 * @param code
	 */
	public static void jump(Activity activity,Class<?> clazz,int code)
	{
//		AppCache.getInstance().clear();
//		Intent intent = new Intent(activity,LoginActivity.class);
//		intent.putExtra(LoginActivity.JUMP_SOURCE, clazz);
//		activity.startActivityForResult(intent,code);
	}
	

	public static void jump(Fragment fragment,int code)
	{
//		AppCache.getInstance().clear();
// 		Intent intent = new Intent();
// 		intent.setClass(fragment.getActivity(), LoginActivity.class);
// 		fragment.startActivityForResult(intent,code);
	}

	public static void jump(Activity activity,int code)
	{
		//AppCache.getInstance().clear();
 		Intent intent = new Intent();
 		intent.setClass(activity, LoginActivity.class);
  		activity.startActivityForResult(intent,code);

  	}
 
	
	
}
